/**
 * <p>Copyright: Copyright (c) 2014</p>
 * <p>Company: ---Technology Co. Ltd</p>
 * <p>2014-4-30</p>
 * @author Jan.Zhang
 */
package com.xianl.busiplat.controller;

import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xianl.busiplat.common.SecurityConstants;
import com.xianl.busiplat.dwz.AjaxObject;
import com.xianl.busiplat.po.Module;
import com.xianl.busiplat.po.User;
import com.xianl.busiplat.service.ModuleService;
import com.xianl.busiplat.service.UserRoleService;
import com.xianl.busiplat.service.UserService;
import com.xianl.busiplat.shiro.ShiroDbRealm;


/** 
 * desc:
 * <p>author: Jan.Zhang </p>
 * date: 2014-4-30
 */
@Controller
@RequestMapping("/sys/index")
public class IndexController {

	@Autowired
	private UserService userService;
	@Autowired
	private UserRoleService userRoleService;
	@Autowired
	private ModuleService moduleService;
	private static final String INDEX = "sys/index/index";
	private static final String UPDATE_PASSWORD = "sys/index/updatePwd";
	private static final String UPDATE_BASE = "sys/index/updateBase";

	@RequiresAuthentication
	@RequestMapping(value = "", method = RequestMethod.GET)
	public String index(HttpServletRequest request) {
		Subject subject = SecurityUtils.getSubject();
		ShiroDbRealm.ShiroUser shiroUser = (ShiroDbRealm.ShiroUser) subject.getPrincipal();
		Set<String> permissionSet = userRoleService.findPermissions(shiroUser.getId());
		Module menuModule = moduleService.getTree(permissionSet);
		request.getSession().setAttribute(SecurityConstants.LOGIN_USER, shiroUser.getUser());
		String basePath = request.getContextPath();
		request.setAttribute("janTree", janTree(menuModule.getChildren(), basePath));
		return INDEX;
	}

	/**
	 * desc: <p>创建人：Jan.Zhang , 2013-1-9 下午03:26:57</p>
	 * @return
	 */
	private String janTree(List<Module> list, String basePath) {
		StringBuilder sb = new StringBuilder();
		for(Module m: list) {
			sb.append("<div class=\"accordionHeader\">").append("\n");
			sb.append("<h2><span>Folder</span>").append(m.getName()).append("</h2>").append("\n");
			sb.append("</div>").append("\n");
			sb.append("<div class=\"accordionContent\">").append("\n");
			if(null != m.getChildren() ) {
				janMTree(m.getChildren(), sb, basePath);
			}
			sb.append("</div>").append("\n");
		}
		return sb.toString();
	}

	private void janMTree(List<Module> list, StringBuilder sb, String basePath) {
		if(list.isEmpty() )
			return;
		sb.append("<ul class=\"tree treeFolder\">").append("\n");
		for(Module m: list) {
			sb.append("<li>");
			if(!m.getChildren().isEmpty() ) {
				sb.append("<a>").append(m.getName()).append("</a>");
			} else {
				sb.append("<a href=\"").append(basePath).append(m.getUrl())
						.append("\" target=\"navTab\" rel=\"moduleListNav_").append(m.getId()).append("\">")
						.append(m.getName()).append("</a>");
			}
			janMTree(m.getChildren(), sb, basePath);
			sb.append("</li>").append("\n");
		}
		sb.append("</ul>").append("\n");
	}

	@RequestMapping(value = "/updatePwd", method = RequestMethod.GET)
	public String updatePassword() {
		return UPDATE_PASSWORD;
	}

	/**
	 * desc: <p>创建人：Jan.Zhang , 2013-1-14 下午04:21:28</p>
	 * @param request
	 * @param oldPassword
	 * @param plainPassword
	 * @param rPassword
	 * @return
	 */
	@RequestMapping(value = "/updatePwd", method = RequestMethod.POST)
	public @ResponseBody
	String updatePassword(HttpServletRequest request, String oldPassword, String plainPassword, String rPassword) {
		User user = (User) request.getSession().getAttribute(SecurityConstants.LOGIN_USER);
		if(plainPassword.equals(rPassword) ) {
			user.setPlainPassword(plainPassword);
			userService.update(user);
			AjaxObject ajaxObject = new AjaxObject("密码修改成功！");
			return ajaxObject.toString();
		}
		AjaxObject ajaxObject = new AjaxObject("密码修改失败！");
		ajaxObject.setStatusCode(AjaxObject.STATUS_CODE_FAILURE);
		ajaxObject.setCallbackType("");
		return ajaxObject.toString();
	}

	@RequestMapping(value = "/updateBase", method = RequestMethod.GET)
	public String preUpdate() {
		return UPDATE_BASE;
	}

	/**
	 * desc: <p>创建人：Jan.Zhang , 2013-1-14 下午04:21:32</p>
	 * @param user
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/updateBase", method = RequestMethod.POST)
	public @ResponseBody
	String update(User user, HttpServletRequest request) {
		User loginUser = (User) request.getSession().getAttribute(SecurityConstants.LOGIN_USER);
		loginUser.setPhone(user.getPhone());
		loginUser.setEmail(user.getEmail());
		userService.update(loginUser);
		AjaxObject ajaxObject = new AjaxObject("详细信息修改成功！");
		return ajaxObject.toString();
	}
}
